//
//  WPTicketRcodeAlertView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/11.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPTicketRcodeAlertView.h"
#import "WPMakeQRCode.h"
#import "UIImage+RoundedRectImage.h"

@interface WPTicketRcodeAlertView ()
@property (nonatomic, strong) UIScrollView * scrollView;/**< 滚动视图 */
@property (nonatomic, strong) UIImageView * posterImageView;/**< 海报图 */
@property (nonatomic, strong) UILabel * detailTitelLabel;/**< 详细标题 */
@property (nonatomic, strong) UILabel * addressLabel;/**< 地址文本 */
@property (nonatomic, strong) UILabel * timeLabel;/**< 演出时间 */
@property (nonatomic, strong) UILabel * addTopLabel;/**< 取票提醒 */
@property (nonatomic, strong) UIView * rcodeBackView;/**< 二维码背景图 */
@property (nonatomic, strong) UIImageView * RcodeImageView;/**< 二维码图片 */
@property (nonatomic, strong) UILabel * showRcodeLabel;/**< 出示二维码 */
@property (nonatomic, strong) UILabel * tickTimeLabel;/**< 取票时间 */
@property (nonatomic, strong) NSArray * dataSource;/**< 数据源 */
@property (nonatomic, copy) void (^complete)(NSString *result);/**< 结果block */
- (void)initializeAppearance;/**< 一般界面初始化 */
@end
@implementation WPTicketRcodeAlertView
+ (WPTicketRcodeAlertView *)showViewWithSize:(CGRect)rect andWithTitle:(NSString *)title andWithDatasource:(NSArray *)dataSource andWithComplete:(void (^)(NSString *))complete {
    WPTicketRcodeAlertView * view = [[WPTicketRcodeAlertView alloc]initWithTitle:title withSize:rect withDataSource:dataSource WithComplete:complete];
    [view show];
    return view;
}
- (instancetype)initWithTitle:(NSString *)title withSize:(CGRect)rect withDataSource:(NSArray *)arr WithComplete:(void (^)(NSString *))complete {
    self = [super initWithFrame:rect withTitle:title];
    if (self) {
        _complete = complete;
        _dataSource = arr;
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeAppearance {
    [self addSubview:self.scrollView];
    [_scrollView addSubview:self.posterImageView];
    [_scrollView addSubview:self.detailTitelLabel];
    [_scrollView addSubview:self.addressLabel];
    [_scrollView addSubview:self.timeLabel];
    [_scrollView addSubview:self.addTopLabel];
    [_scrollView addSubview:self.rcodeBackView];
    [_scrollView addSubview:self.RcodeImageView];
    [_scrollView addSubview:self.showRcodeLabel];
    [_scrollView addSubview:self.tickTimeLabel];
//    [self addSubview:self.RcodeImageView];
}
#pragma mark - getter
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = ({
            UIScrollView * view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50*DHFlexibleHorizontalMutiplier()+2, CGRectGetHeight(self.bounds), CGRectGetHeight(self.bounds) - 50*DHFlexibleHorizontalMutiplier() -2)];
            view.bounces = NO;
            view.pagingEnabled = NO;
            view.contentSize = CGSizeMake(0, 410*DHFlexibleHorizontalMutiplier());
            view;
        });
    }
    return _scrollView;
}
- (UIImageView *)posterImageView {
    if (!_posterImageView) {
        _posterImageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 0, 60*DHFlexibleHorizontalMutiplier(), 60*DHFlexibleHorizontalMutiplier())];
            view.layer.cornerRadius = 3*DHFlexibleHorizontalMutiplier();
            view.layer.masksToBounds = YES;
            view.image = IMAGE_NAME(_dataSource[0]);
            view;
        });
    }
    return _posterImageView;
}
- (UILabel *)detailTitelLabel {
    if (!_detailTitelLabel) {
        _detailTitelLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.posterImageView.frame)+10*DHFlexibleHorizontalMutiplier(), CGRectGetMinY(self.posterImageView.frame), CGRectGetMaxX(self.bounds)-CGRectGetMaxX(self.posterImageView.frame)-30*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier())];
            lab.numberOfLines = 0;
            lab.textColor = COLOR_RGB(61, 61, 61, 1);
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.text = _dataSource[1];
            lab;
        });
    }
    return _detailTitelLabel;
}
- (UILabel *)addressLabel {
    if (!_addressLabel) {
        _addressLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.detailTitelLabel.frame), CGRectGetMaxY(self.detailTitelLabel.frame), CGRectGetWidth(self.detailTitelLabel.bounds), 20*DHFlexibleHorizontalMutiplier())];
            lab.textColor = COLOR_RGB(102, 102, 102, 1);
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.text = _dataSource[2];
            lab;
        });
    }
    return _addressLabel;
}
- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.detailTitelLabel.frame), CGRectGetMaxY(self.addressLabel.frame), CGRectGetWidth(self.detailTitelLabel.bounds), 20*DHFlexibleHorizontalMutiplier())];
            lab.textColor = COLOR_RGB(102, 102, 102, 1);
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.text = _dataSource[3];
            lab;
        });
    }
    return _timeLabel;
}
- (UILabel *)addTopLabel {
    if (!_addTopLabel) {
        _addTopLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.timeLabel.frame)+20*DHFlexibleHorizontalMutiplier(), CGRectGetWidth(self.bounds), 30*DHFlexibleHorizontalMutiplier())];
            lab.textColor = COLOR_RGB(247, 188, 60, 1);
            lab.font = [UIFont systemFontOfSize:10*DHFlexibleHorizontalMutiplier()];
            lab.textAlignment = NSTextAlignmentCenter;
            lab.text = @"- — — — -  请提前于场馆大门取票点取票  - — — — -";
            lab;
        });
    }
    return _addTopLabel;
}
- (UIView *)rcodeBackView {
    if (!_rcodeBackView) {
        _rcodeBackView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 175*DHFlexibleHorizontalMutiplier(), 175*DHFlexibleHorizontalMutiplier())];
            view.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMaxY(self.addTopLabel.frame)+100*DHFlexibleHorizontalMutiplier());
            view.layer.borderWidth = 1;
            view.layer.borderColor = COLOR_RGB(241, 241, 241, 1).CGColor;
            view;
        });
    }
    return _rcodeBackView;
}
- (UIImageView *)RcodeImageView {
    if (!_RcodeImageView) {
        _RcodeImageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 170*DHFlexibleVerticalMutiplier(), 170*DHFlexibleVerticalMutiplier())];
            view.center = self.rcodeBackView.center;
            CIImage *imgQRCode = [WPMakeQRCode createQRCodeImage:@"www.91ppl.net"];
            
            //使用核心绘图框架CG（Core Graphics）对象操作，进一步针对大小生成二维码图片imgAdaptiveQRCode（图片大小适合，清晰，效果好）
            UIImage *imgAdaptiveQRCode = [WPMakeQRCode resizeQRCodeImage:imgQRCode
                                                                withSize:view.frame.size.width];
            
            //默认产生的黑白色的二维码图片；我们可以让它产生其它颜色的二维码图片，例如：蓝白色的二维码图片
//            imgAdaptiveQRCode = [WPMakeQRCode specialColorImage:imgAdaptiveQRCode

            
            
            
            UIImage *imgIcon = [UIImage createRoundedRectImage:[UIImage imageNamed:@"登录注册_03"]
                                                      withSize:CGSizeMake(40, 40)
                                                    withRadius:10];
            imgAdaptiveQRCode = [WPMakeQRCode addIconToQRCodeImage:imgAdaptiveQRCode
                                                          withIcon:imgIcon
                                                      withIconSize:imgIcon.size];
            view.image = imgAdaptiveQRCode;
            view;
            
        });
    }
    return _RcodeImageView;
}
- (UILabel *)showRcodeLabel {
    if (!_showRcodeLabel) {
        _showRcodeLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.RcodeImageView.frame)+10*DHFlexibleHorizontalMutiplier(), CGRectGetWidth(self.bounds), 20*DHFlexibleHorizontalMutiplier())];
            lab.textColor = COLOR_RGB(61, 61, 61, 1);
            lab.textAlignment = NSTextAlignmentCenter;
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.text = @"取票时请出示二维码";
            lab;
        });
    }
    return _showRcodeLabel;
}
- (UILabel *)tickTimeLabel {
    if (!_tickTimeLabel) {
        _tickTimeLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.showRcodeLabel.frame), CGRectGetWidth(self.bounds), 30*DHFlexibleHorizontalMutiplier())];
            lab.textColor = COLOR_RGB(102, 102, 102, 1);
            lab.textAlignment = NSTextAlignmentCenter;
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.text = _dataSource[4];
            lab;
        });
    }
    return _tickTimeLabel;
}
@end
